from django.conf import settings as _settings
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
import datetime

def settings(request):
    values =  {#'settings': _settings,
            "cur_site":Site.objects.get_current(),
            "cur_theme": _settings.DEFAULT_THEME,
            "style": _settings.DEFAULT_STYLE,
            }
    if request.user.is_authenticated():
        profile = request.user.get_profile()
        for k, v in (("cur_theme", profile.website_theme),
                     ("style", profile.pygments_style)):
            if v != "default":
                values[k] = v
    values["themes"] = [theme for theme in _settings.THEMES
                                                   if theme[0] != "default"]
    return values

def users_online(request):
    now = datetime.datetime.now()
    x_mins = datetime.timedelta(minutes=_settings.MINUTES_UNTIL_OFFLINE)
    users = User.objects.filter(last_login__gt=now-x_mins)
    x = 0 if request.user.is_authenticated() else 1
    show_users = users.filter(profile__show_status__gt=x)
    return {"counted_users_online":users.count(),
            "counted_ghosts": users.count() - show_users.count(),
            "users_online":show_users,
            "minutes_until_offline":_settings.MINUTES_UNTIL_OFFLINE}